Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

synchronize: quotes around arguments #213

Merged
merged 3 commits into from
Jul 14, 2021
Merged

synchronize: quotes around arguments #213

merged 3 commits into from
Jul 14, 2021

Conversation

flybyray
Copy link
Contributor

fix quoting for specific cmd arguments

Fixes:

ISSUE TYPE:

  • Bugfix Pull Request

COMPONENT NAME:
module: synchronize

@Akasurde Akasurde changed the title fixes #24 and #190 synchronize: quotes around arguments Jul 8, 2021
@Akasurde
Copy link
Member

Akasurde commented Jul 8, 2021

cc @quidame @saito-hideki @aminvakil Could you please review this? Thanks in advance.


if rsync_path:
cmd.append('--rsync-path=%s' % rsync_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add tests for this change?

Copy link
Contributor Author

@flybyray flybyray Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test for it.
https://github.com/ansible-collections/ansible.posix/blob/main/tests/integration/targets/synchronize/tasks/main.yml#L154-L166

As @quidame mentioned later the integration tests were disabled sometime ago. I now enabled them. localy with docker it worked

TASK [synchronize : synchronize files using rsync_path (issue#7182)] ***********
changed: [testhost] => {"changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive '--rsync-path=sudo rsync' '--out-format=<<CHANGED>>%i %n%L' /root/ansible_collections/ansible/posix/tests/output/.tmp/output_dir/foo.txt /root/ansible_collections/ansible/posix/tests/output/.tmp/output_dir/foo.rsync_path", "msg": ">f+++++++++ foo.txt\n", "rc": 0, "stdout_lines": [">f+++++++++ foo.txt"]}

TASK [synchronize : assert] ****************************************************
ok: [testhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [synchronize : Cleanup] ***************************************************
changed: [testhost] => (item=foo.rsync_path) => {"ansible_loop_var": "item", "changed": true, "item": "foo.rsync_path", "path": "/root/ansible_collections/ansible/posix/tests/output/.tmp/output_dir/foo.rsync_path", "state": "absent"}

https://dev.azure.com/ansible/ansible.posix/_build/results?buildId=19837&view=logs&j=649027d1-4c8e-54ec-15ed-99c8fd6e3c09&t=e29e47f7-55c5-531b-4273-3a98ea9afdae&l=404

@quidame
Copy link
Contributor

quidame commented Jul 8, 2021

hi @flybyray , thanks for fixing this !
Currently integration tests don't fail because they are disabled in tests/integration/targets/synchronize/aliases. Can you enable them and also add tests related to this PR ?

/pull/213#issuecomment-876480707
Copy link
Collaborator

@saito-hideki saito-hideki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the code changes, I think we probably need to append the integration test like below:

- name: command-line options are quoted correctly (issue#24 and issue#190)
  synchronize:
    src: "{{output_dir}}/foo.txt"
    dest: "dummy:{{output_dir}}/foo.rsync_path_1"
  register: sync_result
  ignore_errors: true
- assert:
    that:
    - sync_result.cmd is search("'--rsh=.*'")
    - sync_result.cmd is search("'--out-format=.*'")

@flybyray
Copy link
Contributor Author

flybyray commented Jul 9, 2021

Based on the code changes, I think we probably need to append the integration test like below:

- name: command-line options are quoted correctly (issue#24 and issue#190)
  synchronize:
    src: "{{output_dir}}/foo.txt"
    dest: "dummy:{{output_dir}}/foo.rsync_path_1"
  register: sync_result
  ignore_errors: true
- assert:
    that:
    - sync_result.cmd is search("'--rsh=.*'")
    - sync_result.cmd is search("'--out-format=.*'")

thanks. it should reference somehow the issues with their reported problems?

Can someone please have a look at those two changes:

https://github.com/ndgit/ansible.posix/blame/fix-24-and-190/plugins/modules/synchronize.py#L604

https://github.com/ndgit/ansible.posix/commit/0118bf0cb9256637684db3f18cbd012d3851065e#diff-3332cfa3b3869c49a3c44104c6662f6e08d303f01ca9491981a2ba38e333a560L604

https://github.com/ndgit/ansible.posix/blame/fix-24-and-190/plugins/modules/synchronize.py#L607

https://github.com/ndgit/ansible.posix/commit/0118bf0cb9256637684db3f18cbd012d3851065e#diff-3332cfa3b3869c49a3c44104c6662f6e08d303f01ca9491981a2ba38e333a560L607

I think they would not be required if the run_command from basic module would work as expected. It looks to me that there is most probably another issue, which should not be solved here.

with my change the command will be run differently:
https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L1856-L1858

The second would be the case with my change an not the first:

If args is a string and use_unsafe_shell=False it will split args to a list and run with shell=False

The implementation might beinefficient. Fist building a string from a list, than building a list from string.

Copy link
Collaborator

@saito-hideki saito-hideki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flybyray Thank you for your comment. As you pointed out, my integration test example is not essential for this PR as it only tests the behavior of shlex_quote().
I also confirmed the integration test works fine now without '--allow-disabled' on my end.

Thanks again :)

@Akasurde Akasurde added the gate Gate PR in Zuul CI (Obsolete: Please set "mergeit" instead of "gate") label Jul 14, 2021
@ansible-zuul ansible-zuul bot merged commit f2601b0 into ansible-collections:main Jul 14, 2021
@flybyray flybyray deleted the fix-24-and-190 branch July 23, 2021 11:47
@alisonatwork
Copy link

@gravesm @Akasurde @saito-hideki in case you didn't notice the bug report - this change has introduced a bug that breaks (formerly) correct and working synchronize tasks. Please can you take a look at the bug report #449 and suggest what to do from here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gate Gate PR in Zuul CI (Obsolete: Please set "mergeit" instead of "gate")
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants